package com.example.service.impl;

import com.example.entity.Building;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Page.PageResult;
import com.example.mapper.BuildingMapper;
import com.example.result.CommonResult;
import com.example.service.BuildingService;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @project: smart-property
 * @description: 楼栋管理 service 实现层
 * @author: cjq
 * @create: 2022-11-17 15:23
 **/
@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    BuildingMapper buildingMapper;

    @Autowired
    RedisUtil redisUtil;

    Integer cid;

    /**
     * 添加/修改楼栋信息
     * @param building
     * @return
     */
    @Override
    public int saveBuildingInfo(Building building) {
        int i = 0;
        String buildingInfo = "buildingInfo"+"_"+cid;
        if (building.getId() == null){
            building.setBuildingNumber(building.getBuildingNumber() + "-" + building.getBuilding());
            building.setDelFlag(0);
            i = buildingMapper.addBuildingInfo(building);
            redisUtil.del(buildingInfo);
        }else {
            building.setBuildingNumber(building.getBuildingNumber() + "-" + building.getBuilding());
            building.setDelFlag(0);
            i = buildingMapper.updateBuildingInfo(building);
            redisUtil.del(buildingInfo);
        }
        return i;
    }

    /**
     * 小区楼座查重
     * @param cid
     * @param building
     * @return
     */
    @Override
    public CommonResult listBuildingName(Integer cid, String building) {
        Building buildingName = buildingMapper.listBuildingName(cid, building);
        if (StringUtils.isEmpty(buildingName)){
            return CommonResult.ok();
        }
        return CommonResult.fail(buildingName);
    }

    /**
     * 楼栋信息列表展示
     * @param purpose
     * @param building
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult queryBuildingInfo(String purpose, String building,Integer cid, PageRequest pageRequest) {
        this.cid = cid;
        String buildingInfo = "buildingInfo"+"_"+this.cid;
        List<Building> list = (List<Building>) redisUtil.get(buildingInfo);
       if (purpose == "" && building == ""){
           if (list == null){
               list = buildingMapper.queryBuildingInfo(purpose,building,cid);
               redisUtil.set(buildingInfo,list);
           }
       }else {
           return MyPageHelper.getPageResult(buildingMapper.queryBuildingInfo(purpose,building,cid));
       }
        return MyPageHelper.getPageResult(list);
    }

    /**
     * 删除楼栋信息
     * @param id
     * @return
     */
    @Override
    public int delBuildingInfo(Integer id) {
        String buildingInfo = "buildingInfo"+"_"+cid;
        redisUtil.del(buildingInfo);
        return buildingMapper.delBuildingInfo(id);
    }

    /**
     * 批量删除楼栋信息
     * @param ids
     * @return
     */
    @Override
    public int batchDel(Integer[] ids) {
        String buildingInfo = "buildingInfo"+"_"+cid;
        redisUtil.del(buildingInfo);
        return buildingMapper.batchDel(ids);
    }
}
